package com.livew.redis.dao.impl;

import javax.annotation.Resource;

import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.stereotype.Repository;

import com.livew.redis.dao.RedisDao;
import com.livew.utils.JsonUtils;

import redis.clients.jedis.Jedis;

@Repository("redisDao")
public class RedisDaoImpl<T> implements RedisDao<T>{

	@Resource
	private JedisConnectionFactory jedisConnectionFactory;
	
	private static Jedis jedis;  
	
	private Jedis getJedis(){  
        if(jedis == null){  
            return jedisConnectionFactory.getShardInfo().createResource();  
        }  
        return jedis;  
    }  

	@Override
	public String get(String key) {
	    String res = 	getJedis().get(key);
		return res;
	}

	@Override
	public void set(String key, String value) {
		this.getJedis().set(key, value);
	}

	@Override
	public void set(String key, T t) {
		this.getJedis().set(key, JsonUtils.toJson(t).toString());
	}  
	
}
